use rusqlite::Connection;
use crate::data::db_path;
use crate::domin::Choose;

#[tauri::command]
pub fn choose_search() -> String{
  let sql = "select id , category_id from tb_category_choose where id = 1 ;";
  let conn = Connection::open(db_path()).expect("choose_search --- expect : get connection error !!!");
  let mut stat = conn.prepare(sql).expect("choose_search --- expect : prepare sql error !!!");
  let rows = stat.query_map( [],|row| {
    Ok(Choose{id:row.get(0).unwrap(),category_id:row.get(1).unwrap()})
  }).expect("choose_search --- expect : execute sql error !!!");
  let mut res = String::new() ;
  let _ = rows.for_each(|row| {
    res = row.unwrap().category_id.to_string();
  });
  res
}

#[tauri::command]
pub fn choose_update(choose: Choose) -> usize{
  let sql = "update tb_category_choose set category_id = ? where id = 1 ;";
  let conn = Connection::open(db_path()).expect("choose_update --- expect : update get connection error !!!");
  conn.execute(sql, [choose.category_id]).expect("choose_update --- expect : update choose data error !!!")
}